/*
 * LifeWindow.java
 * 
 * Copyright (C) 2009 Fred Nicollson, All Rights Reserved.
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * It is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * LifeWindow.java
 *
 * Created on Feb 22, 2009, 11:10:24 PM
 */

package life.gui;

import java.awt.BorderLayout;
import life.states.events.EventState;
import life.model.Board;

/**
 *
 * @author mcfedr
 */
public class LifeWindow extends javax.swing.JFrame {

	EventState dispatch;
	Board board;
	LifeView view;

    /** Creates new form LifeWindow */
    public LifeWindow() {
        initComponents();
		setSize(600, 600);
    }

	public int getSpeed() {
		return 2000 / (speed.getValue() > 0 ? speed.getValue() : 1);
	}

	public void setDispatch(EventState d) {
		dispatch = d;
		if(view != null) view.setDispatch(dispatch);
	}

	public void setBoard(Board b) {
		board = b;
		if(view != null) view.setBoard(board);
	}

	public void setView(LifeView v) {
		if(view != null) jPanel3.remove(view);
		view = v;
		if(dispatch != null) view.setDispatch(dispatch);
		if(board != null) view.setBoard(board);
		jPanel3.add(view, BorderLayout.CENTER);
	}

	@Override
	public void setVisible(boolean v) {
		super.setVisible(v);
		if(view != null) view.updateSize();
	}

	public void update() {
		stepcount.setText("Steps: " + (board!=null?board.getStepCount():0));
		if(view != null) view.updateAll();
	}

	public void setRunningState() {
		clear.setEnabled(false);
		randomise.setEnabled(false);
		step.setEnabled(false);
		runToggle.setText("Pause");
		open.setEnabled(false);
		save.setEnabled(false);
	}

	public void setStoppedState() {
		clear.setEnabled(true);
		randomise.setEnabled(true);
		step.setEnabled(true);
		runToggle.setText("Run");
		open.setEnabled(true);
		save.setEnabled(true);
	}
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        step = new javax.swing.JButton();
        clear = new javax.swing.JButton();
        randomise = new javax.swing.JButton();
        runToggle = new javax.swing.JToggleButton();
        jPanel3 = new javax.swing.JPanel();
        speed = new javax.swing.JSlider();
        jPanel2 = new javax.swing.JPanel();
        stepcount = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        open = new javax.swing.JMenuItem();
        save = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        quit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        gridToggle = new javax.swing.JCheckBoxMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        jPanel1.setLayout(new java.awt.BorderLayout());

        jToolBar1.setRollover(true);

        step.setText("Step");
        step.setFocusable(false);
        step.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        step.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        step.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stepActionPerformed(evt);
            }
        });
        jToolBar1.add(step);

        clear.setText("Clear");
        clear.setFocusable(false);
        clear.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        clear.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearActionPerformed(evt);
            }
        });
        jToolBar1.add(clear);

        randomise.setText("Randomise");
        randomise.setFocusable(false);
        randomise.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        randomise.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        randomise.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                randomiseActionPerformed(evt);
            }
        });
        jToolBar1.add(randomise);

        runToggle.setText("Run");
        runToggle.setFocusable(false);
        runToggle.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        runToggle.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        runToggle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runToggleActionPerformed(evt);
            }
        });
        jToolBar1.add(runToggle);

        jPanel1.add(jToolBar1, java.awt.BorderLayout.NORTH);

        jPanel3.setLayout(new java.awt.BorderLayout());

        speed.setMajorTickSpacing(10);
        speed.setMaximum(50);
        speed.setMinorTickSpacing(1);
        speed.setOrientation(javax.swing.JSlider.VERTICAL);
        speed.setPaintLabels(true);
        speed.setPaintTicks(true);
        speed.setValue(25);
        speed.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                speedStateChanged(evt);
            }
        });
        jPanel3.add(speed, java.awt.BorderLayout.EAST);

        jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        stepcount.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        stepcount.setText("Steps:0");
        jPanel2.add(stepcount);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        jMenu1.setText("File");

        open.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.META_MASK));
        open.setText("Open...");
        open.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openActionPerformed(evt);
            }
        });
        jMenu1.add(open);

        save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.META_MASK));
        save.setText("Save...");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });
        jMenu1.add(save);
        jMenu1.add(jSeparator2);

        quit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.META_MASK));
        quit.setText("Quit");
        quit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitActionPerformed(evt);
            }
        });
        jMenu1.add(quit);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("View");

        gridToggle.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.META_MASK));
        gridToggle.setSelected(true);
        gridToggle.setText("Show Grid");
        gridToggle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gridToggleActionPerformed(evt);
            }
        });
        jMenu2.add(gridToggle);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void runToggleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runToggleActionPerformed
		if(dispatch != null) {
			if(runToggle.isSelected())
				dispatch.run();
			else
				dispatch.stop();
		}
}//GEN-LAST:event_runToggleActionPerformed

	private void stepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stepActionPerformed
		if(dispatch != null) dispatch.step();
}//GEN-LAST:event_stepActionPerformed

	private void openActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openActionPerformed
		if(dispatch != null) dispatch.open();
}//GEN-LAST:event_openActionPerformed

	private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
		if(dispatch != null) dispatch.save();
}//GEN-LAST:event_saveActionPerformed

	private void quitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitActionPerformed
		if(dispatch != null) dispatch.exit();
	}//GEN-LAST:event_quitActionPerformed

	private void gridToggleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gridToggleActionPerformed
		if(view != null) view.getGridManager().setEnabled(gridToggle.isSelected());
}//GEN-LAST:event_gridToggleActionPerformed

	private void speedStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_speedStateChanged
		if(dispatch != null) dispatch.speed(getSpeed());
}//GEN-LAST:event_speedStateChanged

	private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
		if(view != null) view.updateSize();
	}//GEN-LAST:event_formComponentResized

	private void randomiseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_randomiseActionPerformed
		if(dispatch != null) dispatch.randomise();
}//GEN-LAST:event_randomiseActionPerformed

	private void clearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearActionPerformed
		if(dispatch != null) dispatch.clear();
	}//GEN-LAST:event_clearActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton clear;
    private javax.swing.JCheckBoxMenuItem gridToggle;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JMenuItem open;
    private javax.swing.JMenuItem quit;
    private javax.swing.JButton randomise;
    private javax.swing.JToggleButton runToggle;
    private javax.swing.JMenuItem save;
    private javax.swing.JSlider speed;
    private javax.swing.JButton step;
    private javax.swing.JLabel stepcount;
    // End of variables declaration//GEN-END:variables

}
